#!/user/bin/env python
# -*- coding: utf-8 -*-
''' 
Author: Sjy
LastEditTime: 2024-11-04 09:33:03
Description: 使用 superqt 制作一个播放帧数的滑块 控制器

'''

__all__= []

from PyQt5 import QtCore, QtGui, QtWidgets

global PARAMS
PARAMS = {
    "tickInterval": 1,
    "tickPosition": QtWidgets.QSlider.TicksBothSides,
    "orientation": QtCore.Qt.Horizontal,
    "range": (0, 10),
    "value":0,

}

class SliderPlayFrame(QtWidgets.QSlider):
    sliderValueChanged = QtCore.pyqtSignal(int)
    def __init__(self, params:dict=PARAMS, parent=None):
        super(SliderPlayFrame, self).__init__(parent)

        curParams = {**PARAMS, **params}
        self.setOrientation(curParams.get("orientation"))
        self.setRange(*curParams.get("range"))
        self.setValue(curParams.get("value"))
        self.setTickPosition(curParams.get("tickPosition"))
        self.setTickInterval(curParams.get("tickInterval"))
        self.setStyleSheet("""
        QSlider::groove:horizontal {
            border: 1px solid #999999;
            height: 8px; /* the groove expands to the size of the slider by default. by giving it a height, it has a fixed size */
            background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4);
            margin: 2px 0;
        }

        QSlider::handle:horizontal {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);
            border: 1px solid #5c5c5c;
            width: 18px;
            margin: -2px 0; /* handle is placed by default on the contents rect of the groove. Expand outside the groove */
            border-radius: 3px;
        }
        """)

    def _run(self, value):
        if value <= self.maximum() and value >= self.minimum():
            self.setValue(value)

    def valueChanged(self, value):
        print("valueChanged", value)

    # def paintEvent(self, event):
    #     painter = QtGui.QPainter(self)
    #     painter.setRenderHint(QtGui.QPainter.Antialiasing)

    #     for i in range(self.minimum(), self.maximum() + 1, self.tickInterval()):
    #         # pos = self.style().subControlRect(QtWidgets.QSlider, self, QtWidgets.QSlider.SliderHandle, i).center()
    #         # painter.drawText(pos, str(i))
    #         pos = self.minimum() + ((i - self.minimum()) / (self.maximum() - self.minimum())) * self.width()
    #         painter.drawText(pos - 10, 15, str(i))

if __name__ == "__main__":
    
    import sys
    app = QtWidgets.QApplication(sys.argv)

    w = QtWidgets.QWidget()
    w.setWindowTitle("SliderPlayFrame")
    w.resize(400, 100)
    layout = QtWidgets.QVBoxLayout()
    w.setLayout(layout)
    slider = SliderPlayFrame()
    layout.addWidget(slider)
    w.show()

    slider._run(6)
    sys.exit(app.exec_())


